import config from './config'

module.exports = (params) => {
	let url = params.url;
	let method = params.method;
	let header = params.header || null;
	let data = params.data || {};
	//	请求方式 GET POST
	if (method) {
		method = method.toUpperCase(); //	小写转大写
	}
	if (!header){
		header = {
			"content-type": "application/x-www-form-urlencoded",
			'Authorization': uni.getStorageSync("sc_token")
		}
	}
	
	console.log(uni.getStorageSync("sc_token"), "token")
	//	发起请求 加载动画
	if (!params.hideLoading) {
		uni.showLoading({
			title: "加载中"
		})
	}
	//	发起网络请求
	uni.request({
		url: config.url + url,
		method: method || "GET",
		header: header,
		data: data,
		dataType: "json",
		sslVerify: false, //	是否验证ssl证书
		success: res => {
			console.log('res-----', res)
			if (res.statusCode && res.statusCode != 200) {
				uni.showToast({
					icon: "error",
					title: res.msg || '连接超时，请重试'
				})
			}

			if (res.data && res.data.code == 505) {
				//token失效  跳转至登录页
				uni.setStorageSync("sc_token" , "")
			}
			typeof params.success == "function" && params.success(res.data);
		},
		fail: err => {

			uni.showToast({
				icon: "error",
				title: err.msg || '连接超时，请重试'

			})
			typeof params.fail == "function" && params.fail(err.data);
		},
		complete: (e) => {
			console.log("请求完成");
			uni.hideLoading()
			typeof params.complete == "function" && params.complete(e.data);
			return;
		}
	})
}
